package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.entity.pojo.Categories;
import com.entity.pojo.CategoriesRelation;
import com.entity.pojo.Concept;
import com.entity.pojo.ConceptRelation;
import com.mapper.ConceptRelationMapper;
import com.service.ConceptRelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.ConceptService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 关联知识点表（关联关系） 服务实现类
 * </p>
 *
 * @author chenMo
 * @since 2022-04-14
 */
@Service
public class ConceptRelationServiceImpl extends ServiceImpl<ConceptRelationMapper, ConceptRelation> implements ConceptRelationService {
    @Resource
    private ConceptService conceptService;

    @Override
    public List<Concept> getCanBeAddedNextConceptList(Integer currentConceptId, Integer categoryId) {
        // 先查该知识点已有的关联知识点id
        List<Integer> nextConceptIds = baseMapper.selectList(new QueryWrapper<ConceptRelation>()
                .eq("conceptId", currentConceptId))
                .stream()
                .map(ConceptRelation::getNextConceptId)
                .collect(Collectors.toList());
        nextConceptIds.add(currentConceptId);
        return conceptService.list(new QueryWrapper<Concept>()
                .notIn("conceptId", nextConceptIds)
                .eq("categoryId", categoryId)
                .select("conceptId", "conceptName"));
    }

    @Override
    public boolean addConceptRelation(ConceptRelation conceptRelation) {
        baseMapper.insert(conceptRelation);
        return updateNextConceptStr(conceptRelation.getConceptId());
    }

    @Override
    public boolean deleteConceptRelation(Integer conceptId, Integer nextConceptId) {
        baseMapper.delete(new QueryWrapper<ConceptRelation>()
        .eq("conceptId",conceptId)
        .eq("nextConceptId",nextConceptId));
        return updateNextConceptStr(conceptId);
    }

    // 根据知识点id更新知识点表中的关联知识点字符串
    private boolean updateNextConceptStr(Integer conceptId) {
        // 查询该知识点的所有关联知识点id
        List<Integer> nextIdList = baseMapper.selectList(new QueryWrapper<ConceptRelation>()
                .eq("conceptId", conceptId)
                .select("nextConceptId"))
                .stream().map(ConceptRelation::getNextConceptId)
                .collect(Collectors.toList());
        if (nextIdList.isEmpty()) {
            // 如果查询结果为空，则直接置空关联目录字符串
            Concept concept = conceptService.getById(conceptId).setNextConceptStr("");
            return conceptService.updateById(concept);
        }
        // 如果查询结果不为空，则查询该目录的所有关联目录名称
        List<String> nextNameList = conceptService.listByIds(nextIdList)
                .stream().map(Concept::getConceptName)
                .collect(Collectors.toList());
        // 拼接成字符串，以冒号分割
        String nextNameStr = String.join(":", nextNameList);
        // 查找到该目录在目录表中的记录
        Concept concept = conceptService.getById(conceptId).setNextConceptStr(nextNameStr);
        return conceptService.updateById(concept);
    }
}
